package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import static com.alibaba.fastjson.JSON.parseObject;
import static com.alibaba.fastjson.JSON.toJavaObject;
import static com.cloudbroker.bcs.platform.dav.bizapi.constants.WeiboUrl.GET_WEIBO_ACCESS_TOKEN;
import static com.cloudbroker.bcs.platform.dav.bizapi.constants.WeiboUrl.GET_WEIBO_USERINFO_BY_ACCESS_TOKEN;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.constants.BasicFields;
import com.cloudbroker.bcs.common.util.CollectionUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.ClientErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.WeiboConstant;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.WeiboBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.GetWeiboAccessTokenForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.GetWeiboUserInfoByCodeForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.GetWeiboAccessTokenBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.GetWeiboUserInfoByCodeBackVo;
import com.cloudbroker.bcs.platform.dav.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.dav.common.constant.HttpConstant;
import com.cloudbroker.bcs.platform.dav.common.constant.IFSField;
import com.cloudbroker.bcs.platform.dav.common.util.HttpUtil;
import com.cloudbroker.bcs.platform.dav.middle.extservice.itn.ITNField;

@Service
public class WeiboBizServiceImpl implements WeiboBizService {
    
    private static final Logger logger = LoggerFactory.getLogger(WeiboBizServiceImpl.class);
    
    @Override
    public GetWeiboAccessTokenBackVo getAccessToken(GetWeiboAccessTokenForm form) {
        String appid = form.getClient_id();
        String secret = form.getClient_secret();
        form.putAppid(appid).putSecret(secret).setGrant_type(WeiboConstant.AUTHORIZATION_CODE);
        JSONObject json = parseObject(HttpUtil.doGet(GET_WEIBO_ACCESS_TOKEN, form));
        CollectionUtil.transKey(json, IFSField.ERRCODE, BasicFields.EXT_ERROR_NO);
        CollectionUtil.transKey(json, IFSField.ERRMSG, BasicFields.EXT_ERROR_INFO);
        return toJavaObject(json, GetWeiboAccessTokenBackVo.class);
    }
    
    @Override
    public GetWeiboUserInfoByCodeBackVo getUserinfo(GetWeiboUserInfoByCodeForm form) {
        try {
            JSONObject json = parseObject(HttpUtil.doGet(GET_WEIBO_USERINFO_BY_ACCESS_TOKEN, form));
            CollectionUtil.transKey(json, IFSField.ERRCODE, BasicFields.EXT_ERROR_NO);
            CollectionUtil.transKey(json, IFSField.ERRMSG, BasicFields.EXT_ERROR_INFO);
            GetWeiboUserInfoByCodeBackVo backData = toJavaObject(json, GetWeiboUserInfoByCodeBackVo.class);
            if (backData.getIdstr() == null || "".equals(backData.getIdstr())) {
                return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL, GetWeiboUserInfoByCodeBackVo.class);
            }
            return VOUtil.setSuccessResult(backData);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL, GetWeiboUserInfoByCodeBackVo.class);
        }
    }
    
    @Override
    public GetWeiboUserInfoByCodeBackVo getUserinfoByAccessToken(GetWeiboAccessTokenForm form) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("client_id", ConfigValues.getWeiboAppKey());
        map.put("client_secret", ConfigValues.getWeiboAppSecret());
        map.put("authorization_code", WeiboConstant.AUTHORIZATION_CODE);
        map.put("redirect_uri", ConfigValues.getWeiboRedirectUri());
        map.put("code", form.getCode());
        try {
            JSONObject json = parseObject(HttpUtil.doPost(GET_WEIBO_ACCESS_TOKEN, map, HttpConstant.CHARSET_UTF_8,
                    HttpConstant.CHARSET_UTF_8, ITNField.BASIC));
            GetWeiboUserInfoByCodeForm param = toJavaObject(json, GetWeiboUserInfoByCodeForm.class);
            if (param.getAccess_token() == null || "".equals(param.getAccess_token())) {
                return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL, GetWeiboUserInfoByCodeBackVo.class);
            }
            return getUserinfo(param);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL, GetWeiboUserInfoByCodeBackVo.class);
        }
        
    }
    
}
